<?php 

class Common_Auth{
	private $CI;
	
	public function __construct()
	{
		$this->CI = & get_instance();
	}
	
	public function check_auth()
	{
		if(isset($_COOKIE[CONS_PREFIX. 'user_logged']) && !empty($_COOKIE[CONS_PREFIX. 'user_logged']))
		{
			$cookie = $_COOKIE[CONS_PREFIX. 'user_logged'];
			$cookie = $this->CI->common_string->decode_cookie(json_decode($cookie));
			$user = $this->CI->db->select('username, password, salt, level')->where('username', $cookie)->from('user')->get()->row_array();
			if(count($user))
			{
				if($cookie == $user['username'])
				{
					setcookie(CONS_PREFIX. 'user_logged',json_encode($this->CI->common_string->encode_cookie($user['username'])), $this->CI->common_date->get_today(), '/');
					return array('username' => $user['username'],
								 'password' => $user['password'],
								 'salt' => $user['salt'],
								 'level' => $user['level']);
				}
			}
		}
		return null;
	}
}

?>